package com.think.admin.web.controller;


import com.think.admin.annotations.MenuAnnotation;
import com.think.admin.annotations.ResourceAnnotation;
import com.think.admin.model.mainCompany.MainCompanyDTO;
import com.think.admin.model.mainCompany.MainCompanyQuery;
import com.think.admin.oauth.util.OAuthUtil;
import com.think.admin.web.service.IMainCompanyService;
import com.think.common.utils.ResultUtil;
import com.think.common.utils.SysUtils;
import com.think.common.validation.CreateOrUpdate;
import com.think.common.web.controller.SuperController;
import com.think.dao.entity.MainCompany;
import com.think.dao.enums.ResourcePermissionTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 公司信息表
 * </p>
 *
 * @author cwy
 * @since 2019-07-09
 */
@RestController
@RequestMapping("mainCompany")
@RequiredArgsConstructor
@MenuAnnotation(name = "公司信息", sort = 1, permissionType = ResourcePermissionTypeEnum.PERMISSION)
public class MainCompanyController extends SuperController {

    private final IMainCompanyService mainCompanyService;

    @GetMapping
    @ResourceAnnotation(name = "查看", sort = 1, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity page(MainCompanyQuery mainCompanyQuery) {
        return mainCompanyService.pageMainCompany(getPage(),mainCompanyQuery);
    }

    @PostMapping
    @ResourceAnnotation(name = "新增修改", sort = 2, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity createOrUpdate(@RequestBody @Validated(CreateOrUpdate.class) MainCompany mainCompany) {
        return mainCompanyService.createOrUpdate(mainCompany);
    }

    @GetMapping("/{id}")
    @ResourceAnnotation(name = "公司信息详情", sort = 3, permissionType = ResourcePermissionTypeEnum.LOGIN)
    public HttpEntity get(@PathVariable("id") String id) {
        // 根据id查询
        MainCompany mainCompany = mainCompanyService.getById(id);
        if (SysUtils.isEmpty(mainCompany)) {
            return ResultUtil.getError();
        }

        // 返回
        return ResultUtil.success(mainCompany);
    }

    @PostMapping("/delete")
    @ResourceAnnotation(name = "删除", sort = 4, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity delete(@RequestBody List<String> idList) {
        return mainCompanyService.delete(idList);
    }

    @GetMapping("/all")
    @ResourceAnnotation(name = "公司信息列表", sort = 5, permissionType = ResourcePermissionTypeEnum.LOGIN)
    public HttpEntity all() {
        // 查询可分配的角色
        List<MainCompany> mainCompanyList;

        // 构造返回数据
        List<MainCompanyDTO> mainCompanyDTOList = new ArrayList<>();

        if(SysUtils.isEmpty(OAuthUtil.getUser().getCompanyId())){
            mainCompanyList = mainCompanyService.lambdaQuery()
                    .select(MainCompany::getId, MainCompany::getCompanyName).list();

            mainCompanyList.sort(Comparator.comparing(MainCompany::getId));

            mainCompanyList.forEach(mainCompany -> {
                MainCompanyDTO mainCompanyDTO = new MainCompanyDTO();
                mainCompanyDTO.setId(mainCompany.getId());
                mainCompanyDTO.setCompanyName(mainCompany.getCompanyName());
                mainCompanyDTOList.add(mainCompanyDTO);
            });
        }else{
            MainCompany mainCompany = mainCompanyService.getById(OAuthUtil.getUser().getCompanyId());
            MainCompanyDTO mainCompanyDTO = new MainCompanyDTO();
            mainCompanyDTO.setId(mainCompany.getId());
            mainCompanyDTO.setCompanyName(mainCompany.getCompanyName());
            mainCompanyDTOList.add(mainCompanyDTO);
        }

        // 返回
        return ResultUtil.success(mainCompanyDTOList);
    }
}

